// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue';
import App from './App';
import router from './router/app';
import axios from 'axios';
import Qs from 'qs';
// 引入组件

import './assets/js/component';
require('animate.css');

Vue.config.productionTip = false;

// 创建全局eventbus
Vue.prototype.$bus = new Vue();

const Other = ['login', 'registery','index'];
router.beforeEach((to, from, next) => {
  // 跳转前判断是否登录
  if (Other.join('*').match(to.fullPath.replace('/', '')) || Other.join('*').match(to.name) !== null) {
    next();
  } else {
    Vue.prototype.axios.post('User/index')
    .then(({data}) => {
      // 如果返回值为2000未登录
      console.log(data);
      if (data.reg_status==='123') {
        router.push({path: 'login'});
      } else {
        next();
      }
    })
    .catch(function (error) {
      console.log(error);
    });
  }
});

// axios使用
Vue.prototype.axios = axios.create({
  baseURL: 'http://www.hnprkj.com/index.php/Home/',
  timeout: 10000,
  withCredentials: true,
  transformRequest: [function (data) {
    data = Qs.stringify(data);
    return data;
  }],
  headers: { 'Content-Type': 'application/x-www-form-urlencoded' }
});

// 是否是微信浏览器
Vue.prototype.IsWechat = () => {
  if (navigator.userAgent.toLowerCase().match(/MicroMessenger/i)) {
    return true;
  } else {
    return false;
  }
};

/* eslint-disable no-new */
new Vue({
  el: '#app',
  router,
  components: { App },
  template: '<App/>'
});
